<template>
	<view class="container">
    <view class="list">
      <view class="multi-item">
        <view class="multi-item-child">
           姓名
           <view class="content">
             <input class="uni-input text"  placeholder="填写真实姓名"  v-model="name" />
           </view>
        </view>
        <view class="multi-item-child">
           身份证号
           <view class="content">
             <input type="idcard" class="uni-input text" placeholder="填写身份证号"  v-model="id_card" />
           </view>
        </view>
        <view class="multi-item-child">
           有效期限
           <view class="content">
             <picker mode="date" :value="date" :start="startDate" :end="endDate" @change="bindDateChange">
               <view class="picker-inner">
                   <text class="text">{{date || '请选择'}}</text>
                   <view class="iconfont icon-jiantou _font-10" style="color: #bdbdbd;"></view>
               </view>
             </picker>
           </view>
        </view>
      </view>
    </view>
   
    <view class="multi-operate-box" v-if="tenant_id">
      <view class="left" @tap="handleDelete">删除</view>
      <view class="right" @tap="onSumbmit">保存</view>
    </view>
    <view class="operate-box" @tap="onSumbmit" v-else>保存</view>
	</view>
</template>

<script>
  import { addTenant, editTenant, delTenant } from '@/api/info_setting.js';
  
	export default {
		data() {
			return {
        //  date: this.getDate({
        //     format: true
        // }),
        date: '',
        name: '',
        id_card: '',
        tenant_id: '',
        tenant_info_obj: {}
			}
		},
    computed: {
          startDate() {
              return this.getDate('start');
          },
          endDate() {
              return this.getDate('end');
          }
      },
      onLoad(options){
        console.log('options', options)
        if(options.tenant_info){
           uni.setNavigationBarTitle({
             title: '编辑承租人'
           })
           this.tenant_info_obj =  JSON.parse(decodeURIComponent(options.tenant_info))
           console.log('this.tenant_info_obj', this.tenant_info_obj)
           let {id, name, id_card, validity_period} = this.tenant_info_obj
           this.tenant_id = id
           console.log('tenant_id', this.tenant_id)
           this.name = name
           this.id_card = id_card
           this.date = validity_period
        }else{
          uni.setNavigationBarTitle({
            title: '添加承租人'
          })
        }
      },
      methods: {
              bindDateChange: function(e) {
                  this.date = e.detail.value
              },
              getDate(type) {
                  const date = new Date();
                  let year = date.getFullYear();
                  let month = date.getMonth() + 1;
                  let day = date.getDate();
      
                  if (type === 'start') {
                      year = year - 60;
                  } else if (type === 'end') {
                      year = year + 2;
                  }
                  month = month > 9 ? month : '0' + month;
                  day = day > 9 ? day : '0' + day;
                  return `${year}-${month}-${day}`;
              },
              onSumbmit(){
                  let {name, id_card, date, tenant_id} = this
                  if(!name){
                    uni.showToast({
                      icon: 'none',
                      title: '请填写姓名'
                    })
                    return;
                  }
                  if(!id_card&&id_card.length!==18){
                    uni.showToast({
                      icon: 'none',
                      title: '请正确填写18位身份证号'
                    })
                    return;
                  }
                  if(!date){
                    uni.showToast({
                      icon: 'none',
                      title: '请选择有效期限'
                    })
                    return;
                  }
                  
                  let params = {
                    name: name,
                    id_card: id_card,
                    validity_period: date,
                  }
                  if(tenant_id){
                     params.id = tenant_id
                     editTenant(params).then(res => {
                       console.log('res', res)
                       this.handleSubmitSuccess(res.status, '编辑成功')
                     }).catch((err)=>{
                         uni.showToast({ title:err, icon:'none' })
                     })
                  }else{
                    addTenant(params).then(res => {
                      console.log('res', res)
                      this.handleSubmitSuccess(res.status, '添加成功')
                    }).catch((err)=>{
                        uni.showToast({ title:err, icon:'none' })
                    })
                }  
              },
              handleSubmitSuccess(status, tips = '添加成功'){
                if(status == 200){
                  uni.showToast({
                    icon: 'none',
                    title: tips,
                    success() {
                    	uni.navigateBack()
                    }
                  })
                }else{
                   uni.showToast({ title:res.msg, icon:'none' })
                }
              },
              handleDelete(){
                 let that = this
                 uni.showModal({
                     title: '删除',
                     content: '确认删除吗？',
                     success: function (res) {
                         if (res.confirm) {
                             console.log('用户点击确定');
                             let {tenant_id} = that
                             let params = {}
                             if(tenant_id){
                                params.id = tenant_id
                                delTenant(params).then(res => {
                                  console.log('res', res)
                                  that.handleSubmitSuccess(res.status, '删除成功')
                                }).catch((err)=>{
                                    uni.showToast({ title:err, icon:'none' })
                                })
                             }
                         } else if (res.cancel) {
                             console.log('用户点击取消');
                         }
                     }
                 });
              }
      }
	}
</script>

<style lang="scss" scoped>
	page {
    min-height: 100vh;
		background-color: rgb(248,249,252);
	}

	.container {
		padding: 32rpx;
	}
  .list{
    
  }
  .item{
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: #FFF;
    font-size: 28rpx;
    color: rgba(0,0,0,0.9);
    padding: 32rpx 28rpx;
    border-radius: 10rpx;
    margin-bottom: 24rpx;
  }
  .multi-item{
    width: 686rpx;
    padding: 32rpx 28rpx;
    margin-bottom: 24rpx;
    box-sizing: border-box;
    background: #FFFFFF;
    border-radius: 16rpx 16rpx 16rpx 16rpx;
    .multi-item-child{
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 24rpx 0;
      .content{
        display: flex;
        align-items: center;
        text-align: right;
        .text{
          margin-right: 10rpx;
          font-size: 28rpx;
          color: #333;
          text-align: right;
        }
        .picker-inner{
          display: flex;
          align-items: center;
        }
      }
    }
  }
  .operate-box{
    width: 686rpx;
    height: 96rpx;
    line-height: 96rpx;
    margin: 200rpx auto 30rpx;
    box-sizing: border-box;
    border-radius: 0rpx 0rpx 0rpx 0rpx;
    font-weight: 600;
    font-size: 32rpx;
    color: #FFFFFF;
    background: #008DFF;
    text-align: center;
    border-radius: 12rpx;
  }
  .multi-operate-box{
    display: flex;
    margin: 200rpx auto 30rpx;
    .left{
      width: 327rpx;
      height: 80rpx;
      border: 1rpx solid #008DFF;
      box-sizing: border-box;
      line-height: 80rpx;
      margin-right: 30rpx;
      text-align: center;
      background: #F2F3FF;
      border-radius: 12rpx 12rpx 12rpx 12rpx;
      font-weight: 600;
      font-size: 32rpx;
      color: #008DFF;
    }
    .right{
      width: 327rpx;
      height: 80rpx;
      line-height: 80rpx;
      text-align: center;
      background: #008DFF;
      border-radius: 12rpx 12rpx 12rpx 12rpx;
      font-weight: 600;
      font-size: 32rpx;
      color: #FFFFFF;
    }
  }
</style>
